package com.cat.binaryAnswer;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/minimized-maximum-of-products-distributed-to-any-store/description/
 * @create 2025/8/12 16:57
 * @since JDK17
 */

public class Solution13 {
    boolean check(int[] quantities, int n, int target) {
        int sum = 0, m = quantities.length;
        for (int i = 0; i < m; i++) {   //
            sum += (quantities[i] + target - 1) / target;
            if (sum > n) {
                return false;
            }
        }
        return true;
    }
    public int minimizedMaximum(int n, int[] quantities) {
        int m = quantities.length, max = quantities[0];
        for (int i = 1; i < m; i++) {
            max = Math.max(max, quantities[i]);
        }
        int l = 0, r = max + 1, mid;
        while (l + 1 < r) {
            mid = l + (r - l) / 2;
            if (check(quantities, n, mid)) {
                r = mid;
            } else {
                l = mid;
            }
        }
        return r;
    }
}
